﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMS.cntr;
using SMS.util;
using System.Resources;

namespace SMS.view
{
    public partial class RegistrationForm : Form
    {
        public RegistrationForm()
        {
            InitializeComponent();
        }

        private void RegistrationForm_Load(object sender, EventArgs e)
        {
            bindListOfClasses();
            bindListOfUnregisteredStudents();
            bindListOfRegisteredStudents();
            changeLang(I18nUtil.vnLang);
        }

        public void changeLang(String lang)
        {
            I18nUtil.init(lang, new ResourceManager(I18nUtil.resourcePath, typeof(PreferenceForm).Assembly));

            this.Text = I18nUtil.getString("registerStudents");
            label1.Text = I18nUtil.getString("lblClass");
            groupBox1.Text = I18nUtil.getString("menuStudent");
            groupBox2.Text = I18nUtil.getString("registeredStudents");

            gridViewUnregisteredStudents.Columns[0].HeaderText = I18nUtil.getString("no");
            gridViewUnregisteredStudents.Columns[1].HeaderText = I18nUtil.getString("fullName");

            assignBtn.Text = I18nUtil.getString("btnRegister");
            unassignBtn.Text = I18nUtil.getString("btnUnregister");

            gridViewRegisteredStudents.Columns[0].HeaderText = I18nUtil.getString("no");
            gridViewRegisteredStudents.Columns[1].HeaderText = I18nUtil.getString("fullName");
        }

        private void bindListOfRegisteredStudents()
        {
            gridViewRegisteredStudents.Rows.Clear();
            try
            {
                DataTable table = StudentCntr.Instance.findStudentsIn(Int32.Parse(cmbxClass.SelectedValue.ToString()));

                for (int i = 0; i < table.Rows.Count; i++)
                {

                    gridViewRegisteredStudents.Rows.Add(i + 1, 
                                                        table.Rows[i]["stu_fullname"].ToString(),
                                                        table.Rows[i]["stu_id"].ToString());
                    gridViewRegisteredStudents.Rows[gridViewRegisteredStudents.RowCount - 1].Tag = table.Rows[i];
                }
                gridViewRegisteredStudents.ClearSelection();
            }
            catch (Exception ex) { }
        }

        private void bindListOfUnregisteredStudents()
        {
            gridViewUnregisteredStudents.Rows.Clear();
            try
            {
                DataTable table = StudentCntr.Instance.findStudentsNotIn(Int32.Parse(cmbxClass.SelectedValue.ToString()));

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    gridViewUnregisteredStudents.Rows.Add(i + 1, table.Rows[i]["stu_fullname"].ToString(), table.Rows[i]["stu_id"].ToString());
                    gridViewUnregisteredStudents.Rows[gridViewUnregisteredStudents.RowCount - 1].Tag = table.Rows[i];
                }
                gridViewUnregisteredStudents.ClearSelection();
            }
            catch (Exception ex) { }
        }

        private void bindListOfClasses()
        {
            cmbxClass.DataSource = ClazzCntr.Instance.listAll();
            cmbxClass.DisplayMember = "cla_name";
            cmbxClass.ValueMember = "cla_id";
        }

        private void classCmbx_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindListOfUnregisteredStudents();
            bindListOfRegisteredStudents();
        }

        private void assignBtn_Click(object sender, EventArgs e)
        {
            bool result = true;

            for(int i = 0; i < gridViewUnregisteredStudents.SelectedRows.Count; i++)
            {
                if (gridViewUnregisteredStudents.SelectedRows.Count <= PreferenceCntr.Instance.getClazzCapacity())
                {
                    if (RegistrationCntr.Instance.register(
                            Int32.Parse(gridViewUnregisteredStudents.SelectedRows[i].Cells[2].Value.ToString()),
                            Int32.Parse(cmbxClass.SelectedValue.ToString())) < 0)
                    {
                        MessageBox.Show("Đăng ký lớp không thành công");
                        result = false;
                        break;
                    }
                }
                else 
                {
                    MessageBox.Show("Không được quá " + PreferenceCntr.Instance.getClazzCapacity() + " học sinh");
                }
            }

            if (result)
            {
                MessageBox.Show("Đăng ký lớp thành công");
                bindListOfUnregisteredStudents();
                bindListOfRegisteredStudents();
            }
        }

        private void unassignBtn_Click(object sender, EventArgs e)
        {
            bool result = true;

            for (int i = 0; i < gridViewRegisteredStudents.SelectedRows.Count; i++)
            {
                if (RegistrationCntr.Instance.unregister(
                        Int32.Parse(gridViewRegisteredStudents.SelectedRows[i].Cells[2].Value.ToString()),
                        Int32.Parse(cmbxClass.SelectedValue.ToString())) < 0)
                {
                    MessageBox.Show("Bỏ đăng ký lớp không thành công");
                    result = false;
                    break;
                }
            }

            if (result)
            {
                MessageBox.Show("Bỏ đăng ký lớp thành công");
                bindListOfUnregisteredStudents();
                bindListOfRegisteredStudents();
            }
        }

    }
}
